﻿using AEFunction;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace CommonFunction
{
    public partial class ChangeFieldType : Form
    {
        public List<ExcelField> typeList = new List<ExcelField>();
        private int index = 0;
        public ChangeFieldType(List<ExcelField> ExcelSelectedFields)
        {
            InitializeComponent();
            typeList = ExcelSelectedFields; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridView1.DataSource;
            dt.Clear();
            this.DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void ChangeFieldType_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            foreach (ExcelField ef in typeList)
            {
                DataColumn pDataColumn = new DataColumn(ef.field);
                pDataColumn.Caption = ef.field;
                //字段数据类型
                pDataColumn.DataType = ef.type;
                //字段添加到表中
                dt.Columns.Add(pDataColumn);
            }
            dataGridView1.DataSource = dt;
            comboBox1.SelectedIndex = GetSelectedIndex(dataGridView1.Columns[0].ValueType);
            textBox1.Text = dataGridView1.Columns[0].HeaderText;
            index = 0;
        }

        private int GetSelectedIndex(Type t)
        {
            if (t.ToString() == "System.String")
                return 3;
            else if (t.ToString() == "System.Boolean")
                return 0;
            else if (t.ToString() == "System.Int32")
                return 1;
            else if (t.ToString() == "System.Double")
                return 2;
            else
                return 4;
        }

        private Type GetTypeFromIndex(int index)
        {
            if (index == 3)
                return Type.GetType("System.String");
            else if (index ==0 )
                return Type.GetType("System.Boolean");
            else if (index == 1)
                return Type.GetType("System.Int32");
            else if (index == 2)
                return Type.GetType("System.Double");
            else
                return Type.GetType("System.DataTime");
        }

        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            index = e.ColumnIndex;
            comboBox1.SelectedIndex = GetSelectedIndex(typeList[e.ColumnIndex].type);
            textBox1.Text = dataGridView1.Columns[e.ColumnIndex].HeaderText;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ExcelField ef = typeList[index];
            ef.type = GetTypeFromIndex(comboBox1.SelectedIndex);
            typeList[index] = ef;
            dataGridView1.Columns[index].ValueType = ef.type;
        }
    }
}
